import { lt } from 'lodash';
import { formatDate } from 'microconnect-common/utils/date';

type TDateType = string | number | Date;

export const transDate = (outDate: number) => {
  function checkTime(i: number | string) {
    if (i < 10) {
      i = '0' + i;
    }
    return i;
  }

  let date = new Date(outDate).getTime();
  let days = date / 1000 / 60 / 60 / 24;
  let daysRound = Math.floor(days);
  let hours = date / 1000 / 60 / 60 - 24 * daysRound;
  let hoursRound = Math.floor(hours);
  let minutes = date / 1000 / 60 - 24 * 60 * daysRound - 60 * hoursRound;
  let minutesRound = Math.floor(minutes);
  let seconds = date / 1000 - 24 * 60 * 60 * daysRound - 60 * 60 * hoursRound - 60 * minutesRound;
  let secondsRound = Math.floor(seconds);
  let dayLocalizedText = '天';
  let hourLocalizedText = '时';
  let minuteLocalizedText = '分';
  let secondLocalizedText = '秒';
  let time = `${checkTime(daysRound)}${dayLocalizedText}${checkTime(hoursRound)}${hourLocalizedText}${checkTime(
    minutesRound
  )}${minuteLocalizedText}${checkTime(secondsRound)}${secondLocalizedText}`;
  return time;
};

// 正常用时 # 现在时间 - 开始时间 = 累计时间
// 超时用时 # 现在时间 - 最后时间  = 超时时间
export const getCountDown = (startDate: TDateType, endDate: TDateType) => {
  startDate = formatDate(startDate, 'YYYY/MM/DD hh:mm:ss');
  endDate = formatDate(endDate, 'YYYY/MM/DD hh:mm:ss');

  // 格式化 兼容safari
  startDate = new Date(startDate).getTime();
  endDate = new Date(endDate).getTime();
  let nowDate = new Date().getTime();

  // 如果现在超过entDate = 超时
  let overTime = nowDate >= endDate;
  let countDown = '';

  countDown = isNaN(startDate - endDate) ? '-' : transDate(startDate - endDate);

  return { countDown, overTime };
};
/**
 * @desc 获取某日期到某日期的间隔 45天3小时23分
 * @param curDate
 * @param targetDate
 * @returns
 */
export function getTimeInterval(beginDate?: TDateType, endDate?: TDateType) {
  if (!beginDate || !endDate) return '-';

  const intervalTimestamp = new Date(endDate).getTime() - new Date(beginDate).getTime();
  if (lt(intervalTimestamp, 0)) return '-';

  const DaysText = '天',
    HoursText = '小时',
    MinutesText = '分';
  const dayStr = Math.floor(intervalTimestamp / (1000 * 60 * 60 * 24)) + DaysText;
  const hourStr = Math.floor((intervalTimestamp % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)) + HoursText;
  const minuteStr = Math.floor(((intervalTimestamp % (1000 * 60 * 60 * 24)) % (1000 * 60 * 60)) / (1000 * 60)) + MinutesText;

  return dayStr + hourStr + minuteStr;
}

export const getDateChinese = (date?: TDateType) => {
  if (!date) return '-';

  const dateTime = new Date(date);
  const year = dateTime.getFullYear();
  const month = dateTime.getMonth() + 1;

  return year + '年' + month + '月';
};

export const compareDate = (type: 'min' | 'max', val?: string, val2?: string) => {
  if (!val) return val2 || '';
  if (!val2) return val;
  const date1 = new Date(val).getTime();
  const date2 = new Date(val2).getTime();

  if (type === 'min') {
    return date1 > date2 ? val2 : val;
  }
  return date1 < date2 ? val2 : val;
};

export const formatSecond = (time: any) => {
  let timeStr = '';
  let stringFormat = (i: number) => {
    return i < 10 ? `0${i}` : `${i}`;
  };
  let minuteTime = 0;
  let secondTime = 0;
  let hourTime = 0;
  if (time < 60) {
    timeStr = `00:${stringFormat(time)}`;
  } else if (time >= 60 && time < 3600) {
    minuteTime = parseInt((time / 60).toString());
    secondTime = parseInt((time % 60).toString());
    timeStr = `${stringFormat(minuteTime)}:${stringFormat(secondTime)}`;
  } else if (time >= 3600) {
    let _t = parseInt((time % 3600).toString());
    hourTime = parseInt((time / 3600).toString());
    minuteTime = parseInt((_t / 60).toString());
    secondTime = parseInt((_t % 60).toString());
    timeStr = `${stringFormat(hourTime)}:${stringFormat(minuteTime)}:${stringFormat(secondTime)}`;
  }
  return timeStr;
};

/**
 * 获取本月第一天
 */
export const getNowMonthFirst = () => {
  const _date = new Date();
  _date.setDate(1);
  return _date;
};

/**
 * 获取本月最后一天
 */
export const getNowMonthLast = () => {
  const date = new Date();
  const _date = new Date(date.getFullYear(), date.getMonth() + 1, 0);
  return _date;
};

/**
 * 获取上月第一天
 */
export const getLastMonthFirst = () => {
  const date = new Date();
  const _date = new Date(date.getFullYear(), date.getMonth() - 1, 1);
  return _date;
};

/**
 * 获取上月最后一天
 */
export const getLastMonthLast = () => {
  const date = new Date();
  const _date = new Date(date.getFullYear(), date.getMonth(), 0);
  return _date;
};
